﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FourKill.Engine
{
    public class Sky : IPaintable
    {
        private const int SECTION_COUNT = 6;
        private const double MAGIC_NUMBER = 180;
        public Sky() { }
        public void Paint(IPaintableContext context)
        {            
            try
            {
                int viewWidth = context.ViewPort.PixelSize.Width,
                    skyHeight = context.ViewPort.PixelSize.Height / 2,
                    stripeWidth = viewWidth / SECTION_COUNT;

                Color transparentSkyBlue = Color.FromArgb(240, 135, 206, 235);
                Color transparentLightBlue = Color.FromArgb(200,173,216,230);

                context.BackLayer.FillRectangle(new SolidBrush(transparentSkyBlue), new Rectangle(0, 0, viewWidth, skyHeight));

                List<Rectangle> rects = new List<Rectangle>();
                bool b = false;
                int width = 0;
                for (int x = 0; x < viewWidth; x++)
                {
                    int num = x - Convert.ToInt32(context.Camera.Bearing * MAGIC_NUMBER);

                    int l = num % stripeWidth;
                    if (l < 0) l += stripeWidth; // Reverse.

                    if (l < stripeWidth / 2)
                    {
                        b = true;
                        width++;
                    }
                    else
                    {
                        if (b)
                        {
                            rects.Add(new Rectangle(x - width, 0, width, skyHeight));
                            b = false;
                            width = 0;
                        }
                    }
                }

                rects.Add(new Rectangle(viewWidth - width, 0, width, skyHeight));

                using (SolidBrush sb = new SolidBrush(transparentLightBlue))
                {
                    foreach (var rect in rects)
                    {
                        context.BackLayer.FillRectangle(sb, rect);
                    }
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("***********************************");
                System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
                System.Diagnostics.Debugger.Break();
            }
        }
    }
}
